package com.sec.android.app.myfiles.shadow.android;

import android.drm.DrmInfo;
import android.drm.DrmInfoRequest;
import android.drm.DrmManagerClient;

import org.powermock.api.mockito.PowerMockito;
import org.robolectric.annotation.Implementation;
import org.robolectric.annotation.Implements;

import java.util.Locale;

import static org.powermock.api.mockito.PowerMockito.when;

@Implements(DrmManagerClient.class)
public class ShadowDrmManagerClient {

    /**
     * will return true
     * if file name contain "drm" and
     * ext is dcf, wma, wmv, pya, pyv
     */
    @Implementation
    public boolean canHandle(String path, String mimeType) {
        String fullPath = path.toLowerCase(Locale.getDefault());

        return fullPath.contains("drm") &&
                (fullPath.endsWith("dcf") || fullPath.endsWith("wma") || fullPath.endsWith("wmv") || fullPath.endsWith("pya") || fullPath.endsWith("pyv"));
    }

    @Implementation
    public DrmInfo acquireDrmInfo(DrmInfoRequest drmInfoRequest) {
        DrmInfo drmInfo = PowerMockito.mock(DrmInfo.class);

        String path = (String) drmInfoRequest.get(DrmInfoRequest.SEM_DRM_PATH);

        if (path.toLowerCase(Locale.getDefault()).endsWith(".dcf")) {
            when(drmInfo.get(DrmInfoRequest.SEM_STATUS)).thenReturn(DrmInfoRequest.SEM_SUCCESS);
        } else {
            when(drmInfo.get(DrmInfoRequest.SEM_STATUS)).thenReturn(DrmInfoRequest.SEM_FAIL);
        }

        if (path.toLowerCase(Locale.getDefault()).contains("fl")) {
            when(drmInfo.get("bSendAs")).thenReturn("0");
        } else {
            when(drmInfo.get("bSendAs")).thenReturn("1");
        }

        return drmInfo;
    }

    @Implementation
    public String getOriginalMimeType(String path) {
        String filePath = path.toLowerCase(Locale.getDefault());
        String ret = "";
        if (filePath.contains("image")) {
            ret = "image/*";
        } else if (filePath.contains("audio")) {
            ret = "audio/*";
        } else if (filePath.contains("video")) {
            ret = "video/*";
        }
        return ret;
    }
}
